package com.xgst.bmg.utils;

import org.apache.tomcat.util.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ClassUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.Date;

public class CaptureScreenUtils {
	
	private static final Logger log = LoggerFactory.getLogger(CaptureScreenUtils.class);

	/**
	 * 捕获屏幕，生成图片
	 */
	public static String capture() {
		// 获取屏幕大小
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		String fileName = null ;
		try {
			Robot robot = new Robot();
			Rectangle screenRect = new Rectangle(screenSize);
			// 捕获屏幕
			BufferedImage screenCapture = robot.createScreenCapture(screenRect);
			//存放位置
			String path = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
			fileName = System.currentTimeMillis() + ".jpg" ;
			// 把捕获到的屏幕 输出为 图片
			ImageIO.write(screenCapture, "jpg", new File(path +File.separator + fileName));
			//System.out.println("write "+ new Date().getTime());
			log.info("屏幕截屏路径：{}", path +File.separator + fileName);
		} catch (Exception e) {
			log.error("获取屏幕截屏发生异常", e);
		}
		return fileName;
	}

	/**
	 * 捕获屏幕，转换成流
	 */
	public static InputStream captureStream() {
		// 获取屏幕大小
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		try {
			Robot robot = new Robot();
			Rectangle screenRect = new Rectangle(screenSize);
			// 捕获屏幕
			BufferedImage screenCapture = robot.createScreenCapture(screenRect);
			//图片分辨率压缩(目前不用)，可用于分享屏幕时清晰度调节功能
			//screenCapture = Thumbnails.of(screenCapture).size(1366,768).asBufferedImage();
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			ImageIO.write(screenCapture, "jpg", os);
			InputStream inputStream = new ByteArrayInputStream(os.toByteArray());
			return inputStream;
		} catch (Exception e) {
			log.error("获取屏幕截屏发生异常", e);
		}
		return null;
	}

	/**
	 * 捕获屏幕，转换成Base64
	 */
	public static String captureBase64() {
		// 获取屏幕大小
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		try {
			Robot robot = new Robot();
			Rectangle screenRect = new Rectangle(screenSize);
			// 捕获屏幕
			BufferedImage screenCapture = robot.createScreenCapture(screenRect);
			//图片分辨率压缩(目前不用)，可用于分享屏幕时清晰度调节功能
			//screenCapture = Thumbnails.of(screenCapture).size(1366,768).asBufferedImage();
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			ImageIO.write(screenCapture, "jpg", os);
			String base64 = "data:image/jpg;base64,"+ Base64.encodeBase64String(os.toByteArray());
			System.out.println(new Date().getTime());
			return base64;
		} catch (Exception e) {
			log.error("获取屏幕截屏发生异常", e);
		}
		return null;
	}

	/**
	 * 捕获屏幕为BufferedImage
	 */
	public static BufferedImage captureBufferedImage() {
		// 获取屏幕大小
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		try {
			Robot robot = new Robot();
			Rectangle screenRect = new Rectangle(screenSize);
			// 捕获屏幕
			BufferedImage screenCapture = robot.createScreenCapture(screenRect);
			return screenCapture;
		} catch (Exception e) {
			log.error("获取屏幕截屏发生异常", e);
		}
		return null;
	}


	/**
	 * 将BufferedImage转为base64
	 */
	public static String bufferedImageAsBase64(BufferedImage screenCapture) {
		try {
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			ImageIO.write(screenCapture, "jpg", os);
			String base64 = "data:image/jpg;base64,"+ Base64.encodeBase64String(os.toByteArray());
			return base64;
		} catch (Exception e) {
			log.error("获取屏幕截屏发生异常", e);
		}
		return null;
	}

	/**
	 * 捕获屏幕，生成图片，读取图片，转换成流
	 */
	/*public static String captureIOStream() {
		// 获取屏幕大小
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		String fileName = null ;
		try {
			Robot robot = new Robot();
			Rectangle screenRect = new Rectangle(screenSize);
			// 捕获屏幕
			BufferedImage screenCapture = robot.createScreenCapture(screenRect);
			//存放位置
			String path = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
			fileName = System.currentTimeMillis() + ".jpg" ;
			// 把捕获到的屏幕 输出为 图片
			File file = new File(path +File.separator + fileName);
			ImageIO.write(screenCapture, "jpg", file);
			FileOutputStream fs = new FileOutputStream(file);
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			os.writeTo(fs);
			String base64 = "data:image/jpg;base64,"+ Base64.encodeBase64String(os.toByteArray());
			file.delete();
			System.out.println(new Date().getTime());
			return base64;
		} catch (Exception e) {
			log.error("获取屏幕截屏发生异常", e);
		}
		return null;
	}*/

}
